<?php

declare(strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\Response;
use think\response\Json;
use  app\common\model\WholesaleProduct;
//该文件为自动生成，请按需修改
class Wholesale extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Wholesale';

    //追加虚拟字段
    protected  $append = [
        'start_text',
        'end_text',
    ];

    public function index(): string
    {
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'barDemo,200')
            ->addRightButtons(['edit' => ['size' => '60%'], 'delete'])
            ->addTopButtons(['deleteAll'])
            ->fetch();
    }
    public static function getType()
    {
        return [1=>'上午场', 2=>'下午场'];
    }
    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $start_time = time_to_sec($param['start_time']);
        $end_time = time_to_sec($param['end_time']);
        if ($start_time >= $end_time) {
            return json(['code' => -1, 'msg' => '开始时间不能大于结束时间']);
        }
        if(isset($param['id'])){
            $info = $this->model::find($param['id']);
            if($info->type == 1){
                if($end_time > 43200){
                    return json(['code' => -1, 'msg' => '上午场结束时间不能大于12点']);
                }
            }else{
                if($start_time < 43200){
                    return json(['code' => -1, 'msg' => '下午场结束时间不能小于12点']);
                }
            }
        }
        $param['product_id'] = implode(',',$param['product_id']);
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
    public static function getProList(){
        $arr = [];
        $list = WholesaleProduct::order('id asc')->select();
        foreach($list as $k=>$v){
            $arr[$v['id']] = $v['title'];
        }
        return $arr;
    }
}
